package cn.zhyoung.lesson;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;


public class lesson extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<String> names = new ArrayList<String>();
	private JLabel label = null;
	private Timer timer = null;
	private int index = 0;
	
	public lesson() {
		this.setTitle("test");
		this.setSize(500, 300);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(null);
		
		getNames();
		
		init();
		this.setVisible(true);
	}
	
	
	public void init() {
		JButton startBtn = new JButton("Start");
		startBtn.setBounds(100, 200, 100, 40);
		this.add(startBtn);
		
		JButton stopBtn = new JButton("stop");
		stopBtn.setBounds(300, 200, 100, 40);
		this.add(stopBtn);
		
		label = new JLabel("Test", JLabel.CENTER);
		label.setBounds(100, 80, 300, 30);
		label.setFont(new Font(Font.DIALOG, Font.BOLD, 24));
		this.add(label);
		
		
		timer = new Timer(1000, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				label.setText(names.get(index));
				index++;
				if (index == names.size()) {
					index = 0;
				}
			}
		});
		
		startBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				timer.start();
			}
		});
		
		stopBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				timer.stop();
			}
		});
	}
	
	
	public void getNames() {
//		names.add("1");
//		names.add("2");
//		names.add("3");
	
		try {
			BufferedReader br = new BufferedReader(new FileReader("test.txt"));
			String name = br.readLine();
			while(name != null) {
				names.add(name);
				name = br.readLine();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new lesson();
	}

}
